<script setup>
import {initRoutes, setupRouterGuard} from "./router";
import {onHide, onLaunch, onShow} from "@dcloudio/uni-app";

onLaunch(async () => {
  console.log("App Launch");
  // 在应用启动时进行路由检查
  await initRoutes();
  // 获取当前页面路径并进行权限检查
  const pages = getCurrentPages();
  console.log("pages:", pages);
  const currentPage = pages[pages.length - 1];
  if (currentPage) {
    const guard = setupRouterGuard();
    const canAccess = await guard({url: `/${currentPage.route}`});

    if (!canAccess) {
      // 如果没有权限，重定向到登录页或404
      uni.redirectTo({url: "/pages/login/index"});
    }
  }
});

onShow(() => {
  console.log("App Show");
});

onHide(() => {
  console.log("App Hide");
});
</script>

<style lang="scss">

</style>
